package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.ui.pageslider.PageSliderAdapter;
import com.afkt.project.ui.pageslider.ViewCreateHelper;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import ohos.agp.components.*;

import java.util.ArrayList;

/**
 * detail: Palette 调色板
 *
 * @author Ttt
 *  Palette 基本使用
 * @see //https://www.jianshu.com/p/d3c13eb700a4
 *  Material Design 系列之 Palette 开发详解
 * @see //https://blog.csdn.net/jaynm/article/details/107076754
 */
public class PaletteActivity extends BaseActivity implements TabList.TabSelectedListener {
    private static final String TAG = "PaletteActivity";

    //标题栏
    private DirectionalLayout barLayout;

    private DirectionalLayout bar;

    private Component palette_return;


    private TabList tabList;
    private PageSlider pageSlider;


    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_palette;
    }

    @Override
    protected void initView() {
        barLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_palette_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_palette_bar, null, false);
        barLayout.addComponent(bar);
        palette_return = bar.findComponentById(ResourceTable.Id_palette_return);


        tabList = (TabList) findComponentById(ResourceTable.Id_palette_tablist);
        for (int i = 0; i < 5; i++) {
            TabList.Tab tab = tabList.new Tab(getContext());
            tab.setMultipleLine(true);
            tab.setText("WALLPA" + "\n" + "PER-" + i);
            tabList.addTab(tab);
        }
        tabList.setFixedMode(true);
        tabList.selectTabAt(0);
        tabList.addTabSelectedListener(this);

        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_palette_PageSlider);

        pageSlider.setProvider(new PageSliderAdapter(initPageSliderViewData()));

    }

    private ArrayList<Component> initPageSliderViewData() {
        int pageSize = tabList.getTabCount();
        ArrayList<Component> pages = new ArrayList<>();
        ViewCreateHelper viewCreateHelper = new ViewCreateHelper();
        for (int i = 1; i <= pageSize; i++) {
            pages.add(viewCreateHelper.createPaletteView(this,i));
        }
        return pages;
    }

    @Override
    protected void initListener() {
        palette_return.setClickedListener(component -> {
            LogUtils.error(TAG,"palette_return 按钮点击");
            onBackPressed();
        });

        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                if (tabList.getSelectedTab().getPosition() != i) {
                    tabList.selectTabAt(i);
                }
            }
        });
    }

    @Override
    public void onSelected(TabList.Tab tab) {
        if (pageSlider.getCurrentPage() != tab.getPosition()) {
            pageSlider.setCurrentPage(tab.getPosition());
        }
    }

    @Override
    public void onUnselected(TabList.Tab tab) {

    }

    @Override
    public void onReselected(TabList.Tab tab) {

    }

    @Override
    protected void onBackground() {
        barLayout.removeComponent(bar);
        tabList.removeAllComponents();
        super.onBackground();
    }
}
